#!/usr/bin/env python3
# -*- coding: utf-8 -*-

################################################################################

import csv
from datetime import datetime
from os import sep

from matplotlib.figure import Figure
import matplotlib.pyplot as plt

from matplotlib.pyplot import MultipleLocator

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
import pylab as mpl  #导入中文字体，避免显示乱码

################################################################################
#
# 数据可视化，显示北京2020-1-1至2020-12-31时间内的天气情况
# 数据来源，https://rp5.ru/
# 原始数据保:datasouce/54511.01.01.2020.31.12.2020.1.0.0.cn.utf8.00000000.csv
#
################################################################################

def weather_year():
    """ 北京2020全年最高最低气温
    """
    filename = "54511-1.csv"

    dates, height, low = [], [], []
    with open(filename, encoding="utf-8") as f:
        reader = csv.reader(f, delimiter=";")
        next(reader)

        for row in reader:
            # print("row:{}, rowtype:{}".format(row, type(row)))
            # dates.append(datetime.strptime(row[0], r'%Y-%m-%d'))
            if "11:00" not in row[0]:
                continue

            strpt = datetime.strptime(row[0], r'%d.%m.%Y %H:%M')
            
            dates.append(strpt)

            # print("row[14]:{}".format(row[14]))
            row_14 = row[14]
            # print(row_14)
            if row_14:
                row_14 = float(row_14)
            else:
                row_14 = 0
            low.append(row_14)

            row_15 = row[15]
            if row_15:
                row_15 = float(row_15)
            else:
                row_15 = 0
            height.append(row_15)


    # 设置x轴左右无空隙
    # plt.rcParams['axes.xmargin'] = 0
    # 构建可视化窗口
    # dpi,分辨率
    # figsize, 窗口大小
    fig:Figure = plt.figure(dpi=128, figsize=(10, 6))
    # 设置数据及线条颜色
    plt.plot(dates, height, c='red')
    plt.plot(dates, low, c='blue')
    plt.fill_between(dates, height, low, facecolor="blue", alpha=0.1)

    plt.bar(dates, height, fc='red')
    plt.bar(dates, low, fc='blue')

    for i in range(len(dates)):
        x_d = dates[i]
        y_d = height[i]
        plt.scatter(x_d, y_d, c="red")

        y_d_l = low[i]
        plt.scatter(x_d, y_d_l, c="blue")

    
    
    # 设置标题
    plt.title("Daily high temperatures - 2014", fontsize=24)
    # 设置x轴标签
    plt.xlabel('', fontsize=14)
    # 设置斜的日期格式
    fig.autofmt_xdate()
    # 设置y轴标签
    plt.ylabel("Temperature (F)", fontsize=14)

    # x_major_locator = MultipleLocator(1)
    # #把x轴的刻度间隔设置为1，并存在变量里
    # y_major_locator = MultipleLocator(5)
    # #把y轴的刻度间隔设置为10，并存在变量里
    # ax = plt.gca()
    # #ax为两条坐标轴的实例
    # ax.xaxis.set_major_locator(x_major_locator)
    # #把x轴的主刻度设置为1的倍数
    # ax.yaxis.set_major_locator(y_major_locator)

    # 设置刻度线样式
    # plt.tick_params(axis="both", which="major", labelsize=14)
    plt.show()


if __name__ == "__main__":
    # weather_mouth()

    weather_year()
